home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C013.ZIP / MV.C < prev    next >
Text File  |  1990-01-19  |  3KB  |  114 lines

  1. /********************************************************************
  2.  * C Users Group (U.K) C Source Code Library File CUGLIB.013        *
  3.  * Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd.         *
  4.  * Edgbaston, Birmingham B15 2QN ENGLAND                *
  5.  ********************************************************************
  6.  * File name: mv.c
  7.  * Program name:mv 
  8.  * Source of file: Ron Wellstead
  9.  * Purpose: An MS-DOS copy of the UNIX utility of the same name.
  10.  * Changes: <who what when & why major changes have been made>      
  11.  ********************************************************************/
  12.  
  13. /*
  14.  *
  15.  *    @(#) mv.c 1.2 87/07/27 
  16.  *
  17.  *    UNIX style mv utility for dos
  18.  *
  19.  *    copyright (c) 1987 Ron Wellsted.
  20.  *    This software is provided on the understanding that it is
  21.  *    NOT to be used for commercial gain. It may be freely distributed
  22.  *    in source or object form among amateur and hobby computer users ONLY!
  23.  *
  24.  *    renames or moves files or directories.
  25.  *    usage:    mv file1 file2
  26.  *    or:    mv file1 file2 ... directory
  27.  *    or:    mv directory1 directory2
  28.  *    written for Microsoft C, link with setargv.obj to expand wildcards
  29.  */
  30. #include    <io.h>
  31. #include    <stdio.h>
  32. #include    <sys/types.h>
  33. #include    <sys/stat.h>
  34.  
  35. char *basename();
  36.  
  37. char buffer[128]="@(#)mv.c VR 1.0.0 15 Jul 1987";
  38. struct stat file_stat;
  39.  
  40. main(argc,argv)
  41. int argc;
  42. char *argv[];
  43. {
  44.     int i;
  45.  
  46.     if (argc<3)
  47.     {
  48.         fprintf(stderr,"usage: mv file1 .... fileN|directory\n");
  49.         exit(1);
  50.     }
  51.     if ((stat(argv[argc-1],&file_stat)==0)&&(file_stat.st_mode&S_IFDIR))
  52.     {
  53.         for (i=1;i<argc-1;i++)
  54.         {
  55.             strcpy(buffer,argv[argc-1]);
  56.             strcat(buffer,"\\");
  57.             strcat(buffer,basename(argv[i]));
  58.             if (movefile(argv[i],buffer)) exit(1);
  59.         }
  60.     }
  61.     else
  62.     {
  63.         if (argc>3)
  64.         {
  65.             fprintf(stderr,"mv: %s not directory\n",argv[argc-1]);
  66.             exit (1);
  67.         }
  68.         exit(movefile(argv[1],argv[2]));
  69.     }
  70. }
  71.  
  72. movefile(file1,file2)
  73. char *file1,*file2;
  74. {
  75.     if (access(file1,0))    /* file 1 exists ? */
  76.     {
  77.         fprintf(stderr,"mv: ");
  78.         perror(file1);
  79.         return 1;
  80.     }
  81.     if (access(file2,0)==0)    /* file 2 exists ? */
  82.     {
  83.         if (access(file2,2))    /* writable ? */
  84.         {
  85.             fprintf(stderr,"mv: %s exists, can't remove\n",file2);
  86.             return 1;
  87.         }
  88.         if (unlink(file2))
  89.         {
  90.             fprintf(stderr,"mv: ");
  91.             perror(file2);
  92.             return 1;
  93.         }
  94.     }
  95.     if (rename(file1,file2))
  96.     {
  97.         perror("mv");
  98.         return 1;
  99.     }
  100.     return 0;
  101. }
  102.  
  103. char *basename(str)
  104. char *str;
  105. {
  106.     char *ptr;
  107.  
  108.     for (ptr=str;*ptr!='\0';ptr++)
  109.     {
  110.         if ((*ptr=='\\')||(*ptr=='/')) str=ptr+1;
  111.     }
  112.     return str;
  113. }
  114.